package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.InventoryOrder;
import cn.stylefeng.guns.modular.system.entity.ShiWarehouse;
import cn.stylefeng.guns.modular.system.mapper.InventoryOrderMapper;
import cn.stylefeng.guns.modular.system.model.params.InventoryOrderParam;
import cn.stylefeng.guns.modular.system.service.InventoryOrderService;
import cn.stylefeng.guns.modular.system.service.ShiWarehouseService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 库存表控制器
 *
 * @author 小飞
 * @Date 2019-06-28 13:01:25
 */
@Controller
@RequestMapping("/inventoryOrder")
public class InventoryOrderController extends BaseController {

    private String PREFIX = "/inventoryOrder";

    @Autowired
    private InventoryOrderService inventoryOrderService;
    @Autowired
    private InventoryOrderMapper inventoryOrderMapper;
    @Autowired
    private ShiWarehouseService shiWarehouseService;


    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-28
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/inventoryOrder.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-28
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/inventoryOrder_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-28
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/inventoryOrder_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-28
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(InventoryOrderParam inventoryOrderParam) {
        this.inventoryOrderService.add(inventoryOrderParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-28
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(InventoryOrderParam inventoryOrderParam) {
        this.inventoryOrderService.update(inventoryOrderParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-28
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(InventoryOrderParam inventoryOrderParam) {
        this.inventoryOrderService.delete(inventoryOrderParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-28
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(InventoryOrderParam inventoryOrderParam) {
        InventoryOrder detail = this.inventoryOrderService.getById(inventoryOrderParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-28
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(InventoryOrderParam inventoryOrderParam) {
        return this.inventoryOrderService.findPageBySpec(inventoryOrderParam);
    }


    /**
     * 获取库存列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/getInventoryList")
    public LayuiPageInfo getInventoryList(){
        Page page = LayuiPageFactory.defaultPage();
        Long warhouseId = null;
        IPage pageUse = inventoryOrderMapper.getInventoryList(page,warhouseId);
        return  LayuiPageFactory.createPageInfo(pageUse);
    }

    @RequestMapping("/getInventoryListByWarhouse/{deptId}")
    @ResponseBody
    public LayuiPageInfo  getInventoryListByWarhouse(@PathVariable  Long deptId){

        List<Long> warhouseIds =  shiWarehouseService.getBaseMapper().selectList(new QueryWrapper<ShiWarehouse>()
        .lambda().eq(ShiWarehouse::getPid,deptId)).stream().map(s -> s.getId()).collect(Collectors.toList());
        Page page = LayuiPageFactory.defaultPage();
        Page pageUse = inventoryOrderMapper.getInventory(page,warhouseIds);
        return LayuiPageFactory.createPageInfo(pageUse);
    }






    /**
     * 跳转出库
     */
    @RequestMapping("/chuku")
    public String chuku() {
        return PREFIX + "/chuku.html";
    }






}


